package com.tyam.dao;

import java.util.List;

import com.tyam.bean.Nota;


public class NotaDAOHibernate implements NotaDAO {

	HibernateDAOFactory hibernateDAOFactory = null;
	HibernateDAO<Nota> hibernateDAO = null;

	public NotaDAOHibernate() {
		hibernateDAOFactory = new HibernateDAOFactory();
		hibernateDAO = hibernateDAOFactory.getNotaDAO();
	}
	
	@Override
	public boolean inserir(Nota nota) throws Exception {
		boolean ok = false;

		try {
			hibernateDAO.add(nota);
			ok = true;
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} 
		return ok;
	}

	@Override
	public boolean alterar(Nota nota) throws Exception {
		boolean ok = false;

		try {
			hibernateDAO.update(nota);
			ok = true;
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}

		return ok;
	}

	@Override
	public boolean excluir(Nota nota) throws Exception {
		boolean ok = false;

		try {
			hibernateDAO.remove(nota);
			ok = true;
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} 

		return ok;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Nota> getSelecionar(String nome) {
		String 	sql = " FROM Nota as nota " +
	        	      " WHERE nota.codigoNota <> 0 ";
		if (!(nome.isEmpty())) {
			sql += " AND nota.contribuintePrestador.nome like '" + nome + "%' ";  
		}

		sql += " ORDER BY nota.codigoNota";		

		List<Nota> lista = hibernateDAO.getSession().createQuery(sql).list();

		return lista;		 
	
	}	

	@Override
	public Nota getNota(int codigoNota) {
		return hibernateDAO.get(codigoNota);
	}

	@Override
	public List<Nota> getListarTodos() {
		return hibernateDAO.listAll();
	}


}
